package yznu_Servlet_TSS;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Goods_Utils.Good;
import User_Utils.User;
import yznu_Bean_handle.goodsManager;
import yznu_Bean_handle.infoUpdate;
import yznu_Bean_handle.othersStoreOrder;
import yznu_Bean_handle.vipManager;
import yznu_Bean_handle.mainStoreOrder;
import yznu_Bean_handle.otherStoreManager;
import yznu_utils.Store;

public class TSS_UpdateSer extends HttpServlet {

	
	String name;
	String type;
	String price;
	String url;
	String stock;
	boolean f = false;
	
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		String status = request.getParameter("status");
		request.setAttribute("source", "修改");
		
		if(status.equals("good")){
			
			try {
			
			Good good = new Good();
			good.setName(new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8"));
			good.setType(new String(request.getParameter("type").getBytes("iso-8859-1"),"utf-8"));
			good.setUrl(request.getParameter("url"));
			good.setPrice(request.getParameter("price"));
			good.setStock(request.getParameter("stock"));
			f = new infoUpdate().goodUpdate(good);
			
			} catch (Exception e) {
				// TODO: handle exception
				System.out.println(e);
			}
			
			if(f){
				request.setAttribute("Msg", "修改成功");
				int currentPage=(Integer) request.getSession().getAttribute("currentPage");
				request.getSession().setAttribute("currentPage", currentPage);
				int countPage=new goodsManager().countPage();
				request.getSession().setAttribute("countPage", countPage);
				ArrayList<Good> goods = new goodsManager().selectAll(currentPage);

				request.setAttribute("goods", goods);
				RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/goods.jsp");
				dispatcher.forward(request, response);
			}else{
				request.setAttribute("Msg", "修改失败");
				int currentPage=(Integer) request.getSession().getAttribute("currentPage");
				request.getSession().setAttribute("currentPage", currentPage);
				int countPage=new goodsManager().countPage();
				request.getSession().setAttribute("countPage", countPage);
				ArrayList<Good> goods = new goodsManager().selectAll(currentPage);

				request.setAttribute("goods", goods);
				RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/goods.jsp");
				dispatcher.forward(request, response);
			}
			
		}else if(status.equals("user")){
			
			try {
			
			User user = new User();
			user.setName(new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8"));
			user.setPhone(request.getParameter("phone"));
			user.setEmail(request.getParameter("email"));
			user.setPassword(request.getParameter("password"));
			f = new infoUpdate().userUpdate(user);
			
			} catch (Exception e) {
				// TODO: handle exception
				System.out.println(e);
			}
			
			if(f){
				request.setAttribute("Msg", "修改成功");
				int currentPage=(Integer) request.getSession().getAttribute("currentPage");
				request.getSession().setAttribute("currentPage", currentPage);
				int countPage=new vipManager().countPage();
				request.getSession().setAttribute("countPage", countPage);
				ArrayList<User> users = new vipManager().selectAll(currentPage);

				request.setAttribute("users", users);
				RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/users.jsp");
				dispatcher.forward(request, response);
			}else{
				request.setAttribute("Msg", "修改失败");
				int currentPage=(Integer) request.getSession().getAttribute("currentPage");
				request.getSession().setAttribute("currentPage", currentPage);
				int countPage=new vipManager().countPage();
				request.getSession().setAttribute("countPage", countPage);
				ArrayList<User> users = new vipManager().selectAll(currentPage);

				request.setAttribute("users", users);
				RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/users.jsp");
				dispatcher.forward(request, response);
			}
			
		}else if(status.equals("store")){
			
			try {
			
			Store store = new Store();
			store.setName(new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8"));
			store.setPhone(request.getParameter("phone"));
			store.setAddress(new String(request.getParameter("address").getBytes("iso-8859-1"),"utf-8"));
			store.setBoss(new String(request.getParameter("boss").getBytes("iso-8859-1"),"utf-8"));
			f = new infoUpdate().storeUpdate(store);
			
			} catch (Exception e) {
				// TODO: handle exception
				System.out.println(e);
			}
			
			if(f){
				request.setAttribute("Msg", "修改成功");
				int currentPage=(Integer) request.getSession().getAttribute("currentPage");
				request.getSession().setAttribute("currentPage", currentPage);
				int countPage=new otherStoreManager().countPage();
				request.getSession().setAttribute("countPage", countPage);
				ArrayList<Store> stores = new otherStoreManager().selectAll(currentPage);

				request.setAttribute("stores", stores);
				RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/stores.jsp");
				dispatcher.forward(request, response);
			}else{
				request.setAttribute("Msg", "修改失败");
				int currentPage=(Integer) request.getSession().getAttribute("currentPage");
				request.getSession().setAttribute("currentPage", currentPage);
				int countPage=new otherStoreManager().countPage();
				request.getSession().setAttribute("countPage", countPage);
				ArrayList<Store> stores = new otherStoreManager().selectAll(currentPage);

				request.setAttribute("stores", stores);
				RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/stores.jsp");
				dispatcher.forward(request, response);
			}
			
		}
		else
			System.out.println("status");
		
		
		
		
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

}
